package com.ifp.opengate.boot.task;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import com.ifp.opengate.boot.utils.RedisUtils;

/**
 * <p>
 * api分组信息初始化
 * </P>
 *
 * @author zhuzh@belink.com
 * @version 0.0.1
 * @className ApiGroupCacheDataTask
 * @sine 2020/5/21 14:30
 */
@Component
public class ApiGroupCacheDataTask implements ApplicationRunner {

    @Autowired
    RedisUtils redisUtils;

    @Value("${opengate_group_api_group_redis_key}")
    String apiGroupRedisKey;

    Map<String, Map<Object, Object>> apiGroupInfoMap = new HashMap<>();

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Map<Object, Object> apiGroupkeys = redisUtils.hmget(apiGroupRedisKey);
        apiGroupkeys.keySet().parallelStream().forEach(it -> {
            this.addApiGroupInfo((String)it);
        });
    }

    /**
     * 添加API分组
     * 
     * @param apiGroupKey
     */
    private void addApiGroupInfo(String apiGroupKey) {
        Map<Object, Object> apiGroupInfo = redisUtils.hmget(apiGroupKey);
        apiGroupInfoMap.put(apiGroupKey, apiGroupInfo);
    }

    public Map<String, Map<Object, Object>> getApiGroupInfoMap() {
        return apiGroupInfoMap;
    }

    public void setApiGroupInfoMap(Map<String, Map<Object, Object>> apiGroupInfoMap) {
        this.apiGroupInfoMap = apiGroupInfoMap;
    }

    /**
     * 删除API分组
     * 
     * @param apiGroupKey
     */
    public void delApiGroupInfo(String apiGroupKey) {
        apiGroupInfoMap.remove(apiGroupKey);
    }

    public void initAlerting() {
        // HashMap<String, Object> user1 = new HashMap<String, Object>(4) {{
        //     put("id", 9);
        //     put("user_name", "宋妍霏");
        //     put("user_phone", "1433");
        //     put("user_email", "syf@test.com");
        // }};
        //
        // HashMap<String, Object> user2 = new HashMap<String, Object>(4) {{
        //     put("id", 10);
        //     put("user_name", "刘闻钦");
        //     put("user_phone", "14334455");
        //     put("user_email", "lwq@test.com");
        // }};
        //
        // ArrayList<Map<String, Object>> list = new ArrayList<>();
        // list.add(user1);
        // list.add(user2);
        //
        // Map<String, Object> map = new HashMap<String, Object>(2) {{
        //     put("Ratelimiting", list);
        //     put("CircuitBreaker",  list);
        // }};
        //
        // redisUtils.set("opengate_alerting_info", SerializeUtils.serialize(map));
        String jsonString = "{\"Ratelimiting\":[{\"id\":9,\"user_name\":\"宋妍霏\",\"user_phone\":\"1433\",\"user_email\":\"1374393272@qq.com\"},{\"id\":10,\"user_name\":\"刘闻钦\",\"user_phone\":\"14334455\",\"user_email\":\"suremotoo@163.com\"}],\"CircuitBreaker\":[{\"id\":9,\"user_name\":\"宋妍霏\",\"user_phone\":\"1433\",\"user_email\":\"1374393272@qq.com\"},{\"id\":10,\"user_name\":\"刘闻钦\",\"user_phone\":\"14334455\",\"user_email\":\"suremotoo@163.com\"}]}";
        redisUtils.set("opengate_alerting_info", jsonString);
    }
}
